package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;
import java.util.*;

/**
 * @program: crm
 * @description:
 * @author: Mr.chen
 * @create: 2018-07-07 21:45
 **/
@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper mapper;
    @Autowired
    private RequestMappingHandlerMapping rmhm;


    @Override
    public List<Permission> list() {
        return mapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int total = mapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<Permission> list = mapper.queryForList(qo);
        return new PageResult(total, list);
    }

    @Override
    public void reload() {
        // 查询出所有的权限表达式
        List<Permission> list = mapper.selectAll();
        Set<String> set = new HashSet<>();
        for (Permission permission : list) {
            set.add(permission.getResource());
        }

        Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
        Collection<HandlerMethod> values = handlerMethods.values();
        RequiresPermissions anno = null;
        Permission permission = null;

        for (HandlerMethod method : values) {
            anno = method.getMethodAnnotation(RequiresPermissions.class);
            if (anno != null) {
                //RequiresPermissions value是一个数组, 只需要第一个即可
                String resource = anno.value()[0];
                if (set.contains(resource)) {
                    continue;
                }
                String name = method.getMethodAnnotation(PermissionName.class).value();
                permission = new Permission();
                permission.setName(name);
                permission.setResource(resource);
                mapper.insert(permission);
            }
        }
    }

    public List<Permission> queryPermissionByRoleId(Long roleId) {
        return mapper.queryPermissionByRoleId(roleId);
    }

    public Set<String> selectAllExpressionByEmpId(Long id) {
        return mapper.selectAllExpressionByEmpId(id);
    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }


}
